﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DBUtility;

namespace SQLServerDAL
{
    /// <summary>
    /// 送货地址簿
    /// </summary>
    public class ShippingAddressBase
    {
        private SqlParameter[] Parms;
        private SqlConnection Conn;

        public ShippingAddressBase()
        {
            Conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);
        }

        #region 信息添加
        /// <summary>
        /// 信息添加
        /// </summary>
        /// <param name="_productObj"></param>
        /// <returns></returns>
        public string CreateShippingAddress(Model.ShippingAddressBase _addressObj)
        {
            Parms = new SqlParameter[] {
					new SqlParameter("@ShippingAddressId", SqlDbType.UniqueIdentifier, 16, "Output"),
					new SqlParameter("@UserId", SqlDbType.UniqueIdentifier),
					new SqlParameter("@ShipAddressName", SqlDbType.NVarChar, 512),
					new SqlParameter("@ShipAddress", SqlDbType.NVarChar, 1024),
					new SqlParameter("@ShipCounties", SqlDbType.VarChar, 256),
					new SqlParameter("@ShipCity", SqlDbType.VarChar, 256),
					new SqlParameter("@ShipState", SqlDbType.VarChar, 256),
					new SqlParameter("@ShipCountry", SqlDbType.VarChar, 256),
					new SqlParameter("@ShipZip", SqlDbType.VarChar, 256),
					new SqlParameter("@ShipTel", SqlDbType.VarChar, 256),
					new SqlParameter("@ShipToName", SqlDbType.VarChar, 256),
					new SqlParameter("@ShipCountiesId", SqlDbType.Int),
					new SqlParameter("@ShipCityId", SqlDbType.Int),
					new SqlParameter("@ShipStateId", SqlDbType.Int),
					new SqlParameter("@ShipCountryId", SqlDbType.Int),
					new SqlParameter("@StatusCode", SqlDbType.Int),
					new SqlParameter("@ShipMobile", SqlDbType.VarChar, 256),
                    new SqlParameter("@ReturnValue", SqlDbType.Int)
            };
            Parms[0].Direction = ParameterDirection.Output;
            Parms[1].Value = new Guid(_addressObj.UserId);
            Parms[2].Value = _addressObj.ShipAddressName;
            Parms[3].Value = _addressObj.ShipAddress;
            Parms[4].Value = _addressObj.ShipCounties;
            Parms[5].Value = _addressObj.ShipCity;
            Parms[6].Value = _addressObj.ShipState;
            Parms[7].Value = _addressObj.ShipCountry;
            Parms[8].Value = _addressObj.ShipZip;
            Parms[9].Value = _addressObj.ShipTel;
            Parms[10].Value = _addressObj.ShipToName;
            Parms[11].Value = _addressObj.ShipCountiesId;
            Parms[12].Value = _addressObj.ShipCityId;
            Parms[13].Value = _addressObj.ShipStateId;
            Parms[14].Value = _addressObj.ShipCountryId;
            Parms[15].Value = _addressObj.StatusCode;
            Parms[16].Value = _addressObj.ShipMobile;
            Parms[17].Direction = ParameterDirection.ReturnValue;

            string result = "";
            int status = 0;
            try
            {
                SqlHelper.ExecuteNonQuery(Conn, CommandType.StoredProcedure, "ShippingAddressBase_Create", Parms);
                status = ((Parms[17].Value != null) ? ((int)Parms[17].Value) : -1);
                if (status == 0)
                {
                    result = "succeeded";
                    if (Common.Utils.IsGuid(Parms[0].Value.ToString()))
                        _addressObj.ShippingAddressId = Parms[0].Value.ToString();

                    result += "|" + _addressObj.ShippingAddressId;
                }
                else if (status == -1)
                {
                    result = "存储过程执行错误!"; /// storedprocedure error
                }
            }
            catch (Exception err)
            {
                result = err.Message;
            }
            finally
            {
                Conn.Close();
                Conn.Dispose();
            }
            return result;
        }
        #endregion

        #region 信息修改
        /// <summary>
        /// 信息修改
        /// </summary>
        /// <param name="_productObj"></param>
        /// <returns></returns>
        public string ModifyShippingAddress(Model.ShippingAddressBase _addressObj)
        {
            Parms = new SqlParameter[] {
					new SqlParameter("@ShippingAddressId", SqlDbType.UniqueIdentifier),
					new SqlParameter("@UserId", SqlDbType.UniqueIdentifier),
					new SqlParameter("@ShipAddressName", SqlDbType.NVarChar, 512),
					new SqlParameter("@ShipAddress", SqlDbType.NVarChar, 1024),
					new SqlParameter("@ShipCounties", SqlDbType.VarChar, 256),
					new SqlParameter("@ShipCity", SqlDbType.VarChar, 256),
					new SqlParameter("@ShipState", SqlDbType.VarChar, 256),
					new SqlParameter("@ShipCountry", SqlDbType.VarChar, 256),
					new SqlParameter("@ShipZip", SqlDbType.VarChar, 256),
					new SqlParameter("@ShipTel", SqlDbType.VarChar, 256), 
					new SqlParameter("@ShipMobile", SqlDbType.VarChar, 256),
					new SqlParameter("@ShipToName", SqlDbType.VarChar, 256),
					new SqlParameter("@ShipCountiesId", SqlDbType.Int),
					new SqlParameter("@ShipCityId", SqlDbType.Int),
					new SqlParameter("@ShipStateId", SqlDbType.Int),
					new SqlParameter("@ShipCountryId", SqlDbType.Int),
					new SqlParameter("@StatusCode", SqlDbType.Int),
                    new SqlParameter("@ReturnValue", SqlDbType.Int)
            };
            Parms[0].Value = new Guid(_addressObj.ShippingAddressId);
            Parms[1].Value = new Guid(_addressObj.UserId);
            Parms[2].Value = _addressObj.ShipAddressName;
            Parms[3].Value = _addressObj.ShipAddress;
            Parms[4].Value = _addressObj.ShipCounties;
            Parms[5].Value = _addressObj.ShipCity;
            Parms[6].Value = _addressObj.ShipState;
            Parms[7].Value = _addressObj.ShipCountry;
            Parms[8].Value = _addressObj.ShipZip;
            Parms[9].Value = _addressObj.ShipTel;
            Parms[10].Value = _addressObj.ShipMobile;
            Parms[11].Value = _addressObj.ShipToName;
            Parms[12].Value = _addressObj.ShipCountiesId;
            Parms[13].Value = _addressObj.ShipCityId;
            Parms[14].Value = _addressObj.ShipStateId;
            Parms[15].Value = _addressObj.ShipCountryId;
            Parms[16].Value = _addressObj.StatusCode; 
            Parms[17].Direction = ParameterDirection.ReturnValue;

            string result = "";
            int status = 0;
            try
            {
                SqlHelper.ExecuteNonQuery(Conn, CommandType.StoredProcedure, "ShippingAddressBase_Update", Parms);
                status = ((Parms[17].Value != null) ? ((int)Parms[17].Value) : -1);
                if (status == 0)
                {
                    result = "succeeded";
                    result += "|" + _addressObj.ShippingAddressId;
                }
                else if (status == -1)
                {
                    result = "存储过程执行错误!"; ///storedprocedure error
                }
            }
            catch (Exception err)
            {
                result = err.Message;
            }
            finally
            {
                Conn.Close();
                Conn.Dispose();
            }
            return result;
        }
        #endregion

        #region 获取信息实体
        /// <summary>
        /// 获取信息实体
        /// </summary>
        /// <param name="shippingAddressId"></param>
        /// <returns></returns>
        public Model.ShippingAddressBase GetObjectById(string shippingAddressId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ShippingAddressBase WHERE ShippingAddressId = @ShippingAddressId");

            SqlParameter[] Parms = new SqlParameter[] { new SqlParameter("@ShippingAddressId", SqlDbType.UniqueIdentifier) };
            Parms[0].Value = new Guid(shippingAddressId);

            Model.ShippingAddressBase _addressObj = null;
            try
            {
                SqlDataReader rd = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql.ToString(), Parms);
                if (rd.Read())
                {
                    _addressObj = new Model.ShippingAddressBase();
                    _addressObj.UserId = rd["UserId"].ToString();
                    _addressObj.ShipAddressName = rd["ShipAddressName"].ToString();
                    _addressObj.ShipAddress = rd["ShipAddress"].ToString();
                    _addressObj.ShipCounties = rd["ShipCounties"].ToString();
                    _addressObj.ShipCity = rd["ShipCity"].ToString();
                    _addressObj.ShipState = rd["ShipState"].ToString();
                    _addressObj.ShipCountry = rd["ShipCountry"].ToString();
                    _addressObj.ShipZip = rd["ShipZip"].ToString();
                    _addressObj.ShipTel = rd["ShipTel"].ToString();
                    _addressObj.ShipMobile = rd["ShipMobile"].ToString();
                    _addressObj.ShipToName = rd["ShipToName"].ToString();
                    _addressObj.ShipCountiesId = int.Parse(rd["ShipCountiesId"].ToString());
                    _addressObj.ShipCityId = int.Parse(rd["ShipCityId"].ToString());
                    _addressObj.ShipStateId = int.Parse(rd["ShipStateId"].ToString());
                    _addressObj.ShipCountryId = int.Parse(rd["ShipCountryId"].ToString());
                    _addressObj.StatusCode = int.Parse(rd["StatusCode"].ToString());
                }
            }
            catch
            {
                return null;
            }
            return _addressObj;
        }
        #endregion
 
        #region 删除信息
        /// <summary>
        /// 匹配 ID 删除
        /// isLogical = True 逻辑删除, isLogical = false 物理删除
        /// </summary>
        public void Delete(string shippingAddressId)
        {
            StringBuilder strSql = new StringBuilder();
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ShippingAddressId", SqlDbType.UniqueIdentifier) };
            parameters[0].Value = new Guid(shippingAddressId);

            strSql.Append("DELETE FROM ShippingAddressBase WHERE [ShippingAddressId] = @ShippingAddressId");
            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql.ToString(), parameters);
        }
        #endregion
    }
}
